package com.example.teacher_app.vm.home;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.teacher_app.R;
import com.example.teacher_app.databinding.AdvCardItemLayoutBinding;
import com.example.teacher_app.databinding.ImageCardItemLayoutBinding;
import com.example.teacher_app.databinding.InfomationCardItemLayoutBinding;
import com.example.teacher_app.databinding.TextCardItemLayoutBinding;
import com.example.teacher_app.databinding.VideoCardItemLayoutBinding;


import java.util.List;

import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;

import static com.example.teacher_app.vm.home.RootBean.*;


public class InfoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<RootBean> list;

    public InfoAdapter(List<RootBean> list) {
        this.list = list;
    }

    private Context context;

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        if (viewType == MSE_CARD){
            return new MsgCardHolder(LayoutInflater.from(context).inflate(R.layout.infomation_card_item_layout,parent,false));
        }else if (viewType == IMAGE){
            return new ImageHolder(LayoutInflater.from(context).inflate(R.layout.image_card_item_layout,parent,false));
        }else if (viewType == VIDEO){
            return new VideoHolder(LayoutInflater.from(context).inflate(R.layout.video_card_item_layout,parent,false));
        }else if (viewType == TEXT){
            return new TextHolder(LayoutInflater.from(context).inflate(R.layout.text_card_item_layout,parent,false));
        }else if (viewType == ADV){
            return new AdvHolder(LayoutInflater.from(context).inflate(R.layout.adv_card_item_layout,parent,false));
        }else return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MsgCardHolder){
            Glide.with(context).load(list.get(position).getFirst_image_url()).centerCrop().into(((MsgCardHolder) holder).binding.firstImage);
            Glide.with(context).load(list.get(position).getIcon()).centerCrop().centerCrop().into(((MsgCardHolder) holder).binding.icon);
            ((MsgCardHolder) holder).binding.description.setText(list.get(position).getContent());
            ((MsgCardHolder) holder).binding.title.setText(list.get(position).getTitle());
        }else if (holder instanceof  VideoHolder){
            ((VideoHolder) holder).binding.title.setText(list.get(position).getContent());
            ((VideoHolder) holder).binding.videoplayer.setUp(list.get(position).getVideo_url()
                    , JCVideoPlayerStandard.SCREEN_LAYOUT_NORMAL, list.get(position).getTitle());
            Glide.with(context).load(list.get(position).getFirst_image_url()).into(((VideoHolder) holder).binding.videoplayer.thumbImageView);
        }else if (holder instanceof TextHolder){
            ((TextHolder) holder).binding.title.setText(list.get(position).getTitle());
            ((TextHolder) holder).binding.content.setText(list.get(position).getContent());
        }else if (holder instanceof  ImageHolder){
            ((ImageHolder) holder).binding.title.setText(list.get(position).getTitle());
            if (list.get(position).getFirst_image_url() != null){
                Glide.with(context)
                        .load(list.get(position).getFirst_image_url())
                        .apply(RequestOptions.bitmapTransform(new GlideRoundTransform(10)))
                        .into(((ImageHolder) holder).binding.firstImage);
            }else {
                ((ImageHolder) holder).binding.firstImage.setVisibility(View.GONE);
            }

            if (list.get(position).getSec_image_url() != null) {
                Glide.with(context)
                        .load(list.get(position).getSec_image_url())
                        .apply(RequestOptions.bitmapTransform(new GlideRoundTransform(10)))
                        .into(((ImageHolder) holder).binding.secImage);
            }else {
                ((ImageHolder) holder).binding.secImage.setVisibility(View.GONE);
            }

            if (list.get(position).getThird_image_url() != null) {
                Glide.with(context)
                        .load(list.get(position).getThird_image_url())
                        .apply(RequestOptions.bitmapTransform(new GlideRoundTransform(10)))
                        .into(((ImageHolder) holder).binding.thirdImage);
            }else {
                ((ImageHolder) holder).binding.thirdImage.setVisibility(View.GONE);
            }

        }else if (holder instanceof AdvHolder){
            Glide.with(context)
                    .load(list.get(position).getAdv_image_url())
                    .centerCrop()
                    .into(((AdvHolder) holder).binding.advImage);
        }

        holder.itemView.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.setAction("com.ugrow.ugrowcommunity.activity.web.WebActivity");
            intent.putExtra("title",list.get(position).getTitle());
            intent.putExtra("url",list.get(position).getJump_url());
            context.startActivity(intent);
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class MsgCardHolder extends RecyclerView.ViewHolder{
        InfomationCardItemLayoutBinding binding;
        public MsgCardHolder(@NonNull View itemView) {
            super(itemView);
            binding = InfomationCardItemLayoutBinding.bind(itemView);
        }
    }
    static class VideoHolder extends RecyclerView.ViewHolder{
        VideoCardItemLayoutBinding binding;
        public VideoHolder(@NonNull View itemView) {
            super(itemView);
            binding = VideoCardItemLayoutBinding.bind(itemView);
        }
    }
    static class ImageHolder extends RecyclerView.ViewHolder{
        ImageCardItemLayoutBinding binding;
        public ImageHolder(@NonNull View itemView) {
            super(itemView);
            binding = ImageCardItemLayoutBinding.bind(itemView);
        }
    }
    static class TextHolder extends RecyclerView.ViewHolder{
        TextCardItemLayoutBinding binding;
        public TextHolder(@NonNull View itemView) {
            super(itemView);
            binding = TextCardItemLayoutBinding.bind(itemView);
        }
    }
    static class AdvHolder extends RecyclerView.ViewHolder{
        AdvCardItemLayoutBinding binding;
        public AdvHolder(@NonNull View itemView) {
            super(itemView);
            binding = AdvCardItemLayoutBinding.bind(itemView);
        }
    }


    @Override
    public int getItemViewType(int position) {
        return list.get(position).getType();
    }
}
